package cn.iocoder.yudao.module.erp.service.customeraccount;

import cn.iocoder.yudao.module.erp.dal.dataobject.customeraccount.CustomerAccountDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/*@Service*/
public class DataSyncService {

    @Autowired
    private DataSyncService dataSyncService;

    @Resource
    private CustomerAccountFinishService customerAccountFinishService;

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    // 异步更新 ConcurrentHashMap 中的数据
    @Async
    public void asyncUpdateDataInMap(CustomerAccountDO customerAccountDO) {
        customerAccountFinishService.loadDataFromDB(customerAccountDO);
    }

    // 异步将 ConcurrentHashMap 中的数据同步到数据库
    @Async
    public void asyncSyncDataToDB() {
        customerAccountFinishService.syncDataToDB();
    }

    // 关闭 ExecutorService
    public void shutdownExecutor() {
        executorService.shutdown();
        try {
            if (!executorService.awaitTermination(800, TimeUnit.MILLISECONDS)) {
                executorService.shutdownNow();
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
        }
    }
}
